package com.hyt.it.ogt.kq.service.gov.service.impl;

import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.constant.CodeEnum;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.constant.StringCodeEnum;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.feign.tps.GovTpsClient;
import com.hyt.it.ogt.kq.service.gov.mapper.OfficeTestInventoryMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.OfficeTestInventory;
import com.hyt.it.ogt.kq.service.gov.model.vo.obs.HuaweiObsCopyObjectResultVO;
import com.hyt.it.ogt.kq.service.gov.service.IOfficeTestInventoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;

/**
 * <p>
 *  系统、机构级测试项清单表 服务实现类
 * </p>
 *
 * @author huangyh
 * @since 2022-04-01
 */
@Service
public class OfficeTestInventoryServiceImpl extends BaseServiceImpl<OfficeTestInventoryMapper, OfficeTestInventory> implements IOfficeTestInventoryService {

    @Resource
    private ConfigManager configManager;

    @Resource
    private GovTpsClient tpsClient;

    /**
     * 路径统一分隔符
     */
    public static final String SEPARATOR = "/";

    @Override
    public OfficeTestInventory getByOfficeId(String officeId) {
        return this.lambdaQuery()
                .eq(OfficeTestInventory::getOfficeId, officeId)
                .eq(OfficeTestInventory::getLevel, CodeEnum.OFFICE_DATA.getCode())
                .eq(OfficeTestInventory::getDelFlag, false)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public OfficeTestInventory getSystem() {
        return this.lambdaQuery()
                .eq(OfficeTestInventory::getLevel, CodeEnum.SYSTEM_DATA.getCode())
                .eq(OfficeTestInventory::getDelFlag, false)
                .last("LIMIT 1")
                .one();
    }

    @Override
    public String init(String officeId) {
        //机构下暂无测试项清单数据，获取系统测试项清单数据
        OfficeTestInventory systemTestInventory = this.getSystem();
        //复制系统测试项清单数据，作为机构测试项清单数据
        String officeTestInventoryPath = this.getOfficeTestInventoryPath(officeId);
        ApiResponse<HuaweiObsCopyObjectResultVO> response =
                tpsClient.copyHuaweiObsObject(officeId, systemTestInventory.getPath(), officeTestInventoryPath);
        HuaweiObsCopyObjectResultVO huaweiObsCopyObjectResultVO = response.getData();
        int statusCode = Optional.ofNullable(huaweiObsCopyObjectResultVO)
                .map(HuaweiObsCopyObjectResultVO::getStatusCode)
                .orElse(0);
        if (response.getCode() != 1
                || statusCode != HttpServletResponse.SC_OK) {
            throw new KqException(ResponseCode.TASK_INIT_FAIL.getCode(),
                    "复制系统测试项清单数据至机构中失败");
        }
        //将复制后的系统级测试项清单数据保存至机构中
        OfficeTestInventory officeTestInventory = new OfficeTestInventory();
        officeTestInventory.setLevel(CodeEnum.OFFICE_DATA.getCode())
                .setOfficeId(officeId)
                .setPath(officeTestInventoryPath);
        if (!this.save(officeTestInventory)) {
            throw new KqException(ResponseCode.TASK_INIT_FAIL.getCode(),
                    "将系统级测试项清单数据保存至机构中时，插入系统、机构级测试项清单表失败");
        }
        return officeTestInventoryPath;
    }

    @Override
    public String getOfficeTestInventoryPath(String officeId) {
        //机构级模板文件存储规则：“根目录/模板文件存放目录名/机构级模板目录名/机构id/具体的文件.xxx”
        String cloudRootPath = configManager.getCloudRootPath();
        cloudRootPath = cloudRootPath.substring(0, cloudRootPath.length() - 1);
        return StringUtils.joinWith(SEPARATOR,
                cloudRootPath, configManager.getTemplatePath(),
                configManager.getOfficeTemplatePath(),
                officeId, StringCodeEnum.TEST_INVENTORY_FILE_NAME.getCode());
    }

}
